package com.eveinfo.api.character.skillqueue;

import java.text.ParseException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.eveinfo.api.AbstractApiHandler;
import com.eveinfo.api.ApiError;

public class SkillQueueHandler extends AbstractApiHandler<SkillQueueResponse> {

	public SkillQueueHandler() {
		super();
		response = new SkillQueueResponse();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		String value = (!localName.equals("")) ? localName : qName;
		try {
			if (value.equals("row")) {
				ApiSkillQueue queue = new ApiSkillQueue();
				queue.setPosition(Integer.parseInt(attributes.getValue("queuePosition")));
				queue.setTrainingTypeID(Integer.parseInt(attributes.getValue("typeID")));
				queue.setTrainingToLevel(Integer.parseInt(attributes.getValue("level")));
				queue.setTrainingStartSP(Integer.parseInt(attributes.getValue("startSP")));
				queue.setTrainingDestinationSP(Integer.parseInt(attributes.getValue("endSP")));
				String startTime = attributes.getValue("startTime");
				if(startTime.equals("")){
					queue.setTrainingStartTime(null);
				}else{
					queue.setTrainingStartTime(sdf.parse(startTime));
				}
				String endTime = attributes.getValue("endTime");
				if(endTime.equals("")){
					queue.setTrainingEndTime(null);
				}else{
					queue.setTrainingEndTime(sdf.parse(endTime));
				}
				response.addSkillQueue(queue);
			}
		} catch (ParseException e) {
			error = new ApiError();
			error.setError(e.getMessage());
			e.printStackTrace();
		}
	}

}
